package com.wq.babysecretary.model.impi;

import com.wq.babysecretary.model.BaBayMusicModel;
import com.wq.babysecretary.model.BaBayStoryModel;
import com.wq.babysecretary.model.IWebContentModel;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

/**
 * Created by admin on 2017/8/23.
 */

public class ContentStoryModelImpl implements IWebContentModel {
    public static final String TAG = "http://story.beva.com/99/category/shui-qian";

    public static ContentStoryModelImpl getInstance() {
        return new ContentStoryModelImpl();
    }

    private ContentStoryModelImpl() {

    }

    public Observable<List<BaBayStoryModel>> analyBookcontent(final String s) {
        return Observable.create(new ObservableOnSubscribe<List<BaBayStoryModel>>() {
            @Override
            public void subscribe(ObservableEmitter<List<BaBayStoryModel>> e) throws Exception {
                try {
                    Document doc = Jsoup.connect(s).get();
                    Elements booksE = doc.getElementsByClass("slist").get(0).getElementsByTag("li");
                    if (null != booksE && booksE.size() > 0) {
                        List<BaBayStoryModel> books = new ArrayList<BaBayStoryModel>();
                        List<BaBayStoryModel> baBayStoryModelList = new ArrayList<BaBayStoryModel>();
                        for (Element bookItem : booksE) {
                            BaBayStoryModel item = new BaBayStoryModel();
                            String name = bookItem.getElementsByClass("sll2").select("a").first().text();
                            String link = bookItem.getElementsByClass("sll2").select("a").attr("href");
                            item.setBookName(name);
                            item.setLink("http://story.beva.com" + link);
                            books.add(item);
                        }

                        if (books.size() > 0) {
                            for (int i = 0; i < books.size(); i++) {
                                BaBayStoryModel item = books.get(i);
                                Document docs = Jsoup.connect(books.get(i).getLink()).get();
                                item.setDesc((i + 1) + "");
                                if (docs.getElementById("stcontent") != null) {
                                    item.setBody(docs.getElementById("stcontent").select("p").text());
                                    baBayStoryModelList.add(item);
                                }

                            }
                        }

                        e.onNext(baBayStoryModelList);
                    } else {
                        e.onNext(new ArrayList<BaBayStoryModel>());
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    e.onNext(new ArrayList<BaBayStoryModel>());
                }
                e.onComplete();
            }
        });
    }

    public Observable<List<BaBayMusicModel>> analyMusic(final String s) {
        return Observable.create(new ObservableOnSubscribe<List<BaBayMusicModel>>() {
            @Override
            public void subscribe(ObservableEmitter<List<BaBayMusicModel>> e) throws Exception {
                try {
                    e.onNext(new ArrayList<BaBayMusicModel>());
                } catch (Exception ex) {
                    ex.printStackTrace();
                    e.onNext(new ArrayList<BaBayMusicModel>());
                }
                e.onComplete();
            }
        });
    }
}
